import { Component } from 'react';

import { TextField,Popup } from 'saltui';
const { RightAddon } = TextField;
import AngleRight from 'salt-icon/lib/AngleRight';
import Map from 'components/map';

export default class PageHome extends Component {



  constructor(props) {
    super(props);
    var label = props.label;
    if(!label){
      label = '';
    }
    this.state = {
    };
  }



  componentDidMount() {
    let t = this;
  }


  //点击
  click(){
    let t = this;
    var value = this.props.value;
    if(t.props.readOnly){//只读
      var canShowLocate = this.props.canShowLocate;
      if(canShowLocate == null || canShowLocate == undefined || (typeof(canShowLocate) !== 'boolean' && canShowLocate == '')){
        canShowLocate = true;
      }else{
        canShowLocate = canShowLocate;
      }
      if(value && canShowLocate){
        if(value.latitude && value.longitude){
          //showLocate(value);
          this.instance = Popup.show(
            <Map getPosition={this.getPosition.bind(this)} longitude={value.longitude} latitude={value.latitude} readOnly={true}/>,
              {animationType:'slide-left'}
          );
        }
      }
    }else{
      if(value){
        if(value.latitude && value.longitude){
          this.instance = Popup.show(
            <Map getPosition={this.getPosition.bind(this)} longitude={value.longitude} latitude={value.latitude}/>,
              {animationType:'slide-left'}
          );
          // getLocate(value,function(result){
          //   var name = result.province+result.city+result.adName+result.snippet+result.title;
          //   var returnData = {'name':name,'latitude':result.latitude,'longitude':result.longitude};
          //   t.props.onChange(returnData);
          // });
        }else{
          this.instance = Popup.show(
            <Map getPosition={this.getPosition.bind(this)} longitude={null} latitude={null}/>,
              {animationType:'slide-left'}
          );
          // getLocate(null,function(result){
          //   var name = result.province+result.city+result.adName+result.snippet+result.title;
          //   var returnData = {'name':name,'latitude':result.latitude,'longitude':result.longitude};
          //   t.props.onChange(returnData);
          // });
        }
      }else{
        this.instance = Popup.show(
          <Map getPosition={this.getPosition.bind(this)} longitude={null} latitude={null}/>,
            {animationType:'slide-left'}
        );
        // getLocate(null,function(result){
        //   var name = result.province+result.city+result.adName+result.snippet+result.title;
        //   var returnData = {'name':name,'latitude':result.latitude,'longitude':result.longitude};
        //   t.props.onChange(returnData);
        // });
      }
     
    }
  }

  getPosition(position, address){
    let t = this;
    Popup.hide();
    var lng=117,lat=31;
    if(position != null){
      var positions = (position + "").split(',');
      if(positions.length == 2){
        lng = positions[0];
        lat = positions[1];
      }
    }
    var returnData = {'name':address,'latitude':lat,'longitude':lng};
    t.props.onChange(returnData);
  }
  //初始化ids
  initData(){
  }

  //加载文件列表

  render() {
    const t = this;
    return (
      <div className="" onClick={this.click.bind(this)}>
        <TextField
            required={t.props.required}
            readOnly={true}
            label={t.props.label}
            placeholder={t.props.readOnly?'':t.props.placeholder}
            value={t.props.value == null || t.props.value == undefined ?'':t.props.value.name}
          >
            <RightAddon>
              <AngleRight width={20} height={20} fill="#ccc" className={t.props.readOnly?'t-DN':'demo-t-list-arrow'} />
            </RightAddon>
        </TextField>
      </div>

    );
  }
}

